<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
class Message  extends Base
{
     public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
    public function index()
    {

        if($this->request->isAjax()){
          $param = $this->request->param();
          $type =  isset($param['type'])?$param['type']:''; //请求状态

          $limit = !empty($param['limit'])?intval($param['limit']):'10'; //限制数量
        switch($type){
           case 'unread':
             $where['is_read'] = 0;
           break;
           case 'read':
             $where['is_read'] = 1;
           break;
          }
          $where['to_uid'] = session('uid');
          $where['status'] = 0;
          $order['create_time'] = 'desc';

          $count  = model('message')->where($where)->count();
          $lists  = model('message')->where($where)->order($order)->paginate($limit);

         $list  = $lists->toArray()['data'];
         if(!empty($list)){
            foreach($list as $k=>$v){
               $list[$k]['create_time'] = date('Y-m-d',$v['create_time']);
            }
         }
       
          return json(['code'=>0,'msg'=>'success','count'=>$count,'data'=>$list]);
      }
    //获得所有未读消息
    $where2['is_read'] = 0; //未读
    $where2['status'] = 0; //0 正常状态下的 -1 已经删除
    $where2['to_uid'] = session('uid');
    $unread = model('message')->where($where2)->count();
    
    $this->assign('unread',$unread);

     return $this->fetch();
    }
//消息详情
    public function detail()
    {
       $param = $this->request->param();

       if(!is_numeric($param['mid'])){
          $this->error('当前消息不存或者已被删除！');
       }
       $where['mid'] = intval($param['mid']);
       $message = model('message')->infoData($where);
       $this->assign('message',$message);

       $data['is_read'] = 1;
       $data['update_time'] = time();
       model('message')->editData($data,$where);
       return $this->fetch();
    }

    public function updateRead()
    {
        
        if($this->request->isAjax()){
	        $param= $this->request->param();
	         $where[]= ['mid','IN',$param['id']];
	         $data['is_read'] =1;
	         model('message')->editData($data,$where);
	        return success_json('批量标记阅读成功');        
        }
        


    }
}
